package cn.iocoder.yudao.module.vote.controller.admin.voteuserinfo;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.system.enums.common.SexEnum;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserinfodetail.VoteUserInfoDetailDO;
import cn.iocoder.yudao.module.vote.service.voteuserinfodetail.VoteUserInfoDetailService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.module.vote.enums.ErrorCodeConstants.USER_INFO_NOT_EXISTS;

import cn.iocoder.yudao.module.vote.controller.admin.voteuserinfo.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserinfo.VoteUserInfoDO;
import cn.iocoder.yudao.module.vote.convert.voteuserinfo.VoteUserInfoConvert;
import cn.iocoder.yudao.module.vote.service.voteuserinfo.VoteUserInfoService;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "管理后台 - 投票用户信息")
@RestController
@RequestMapping("/vote/user-info")
@Validated
public class VoteUserInfoController {

    @Resource
    private VoteUserInfoService userInfoService;

    @Resource
    private VoteUserInfoDetailService detailService;


    @PostMapping("/create")
    @ApiOperation("创建投票用户信息")
    @PreAuthorize("@ss.hasPermission('vote:user-info:create')")
    public CommonResult<Integer> createUserInfo(@Valid @RequestBody VoteUserInfoCreateReqVO createReqVO) {
        return success(userInfoService.createUserInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新投票用户信息")
    @PreAuthorize("@ss.hasPermission('vote:user-info:update')")
    public CommonResult<Boolean> updateUserInfo(@Valid @RequestBody VoteUserInfoUpdateReqVO updateReqVO) {
        userInfoService.updateUserInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除投票用户信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('vote:user-info:delete')")
    public CommonResult<Boolean> deleteUserInfo(@RequestParam("id") Integer id) {
        userInfoService.deleteUserInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得投票用户信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('vote:user-info:query')")
    public CommonResult<VoteUserInfoRespVO> getUserInfo(@RequestParam("id") Integer id) {
        VoteUserInfoDO userInfo = userInfoService.getUserInfo(id);
        VoteUserInfoRespVO respVO = VoteUserInfoConvert.INSTANCE.convert(userInfo);
        if(respVO!=null){
            VoteUserInfoDetailDO detailDO = detailService.getDetailByUserId(id);
            if(detailDO!=null){
                respVO.setContent(detailDO.getContent());
                respVO.setFileUrl(detailDO.getFileUrl());
            }
        }
        return success(respVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得投票用户信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('vote:user-info:query')")
    public CommonResult<List<VoteUserInfoRespVO>> getUserInfoList(@RequestParam("ids") Collection<Integer> ids) {
        List<VoteUserInfoDO> list = userInfoService.getUserInfoList(ids);
        return success(VoteUserInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得投票用户信息分页")
    @PreAuthorize("@ss.hasPermission('vote:user-info:query')")
    public CommonResult<PageResult<VoteUserInfoRespVO>> getUserInfoPage(@Valid VoteUserInfoPageReqVO pageVO) {
        PageResult<VoteUserInfoDO> pageResult = userInfoService.getUserInfoPage(pageVO);
        return success(VoteUserInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出投票用户信息 Excel")
    @PreAuthorize("@ss.hasPermission('vote:user-info:export')")
    @OperateLog(type = EXPORT)
    public void exportUserInfoExcel(@Valid VoteUserInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<VoteUserInfoDO> list = userInfoService.getUserInfoList(exportReqVO);
        // 导出 Excel
        List<VoteUserInfoExcelVO> datas = VoteUserInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "投票用户信息.xls", "数据", VoteUserInfoExcelVO.class, datas);
    }

    @PostMapping("/import")
    @ApiOperation("导入投票用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "Excel 文件", required = true, dataTypeClass = MultipartFile.class),
            @ApiImplicitParam(name = "updateSupport", value = "是否支持更新，默认为 false", example = "true", dataTypeClass = Boolean.class)
    })
    @PreAuthorize("@ss.hasPermission('vote:user-info:import')")
    public CommonResult<VoteUserImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                          @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<VoteUserImportExcelVO> list = ExcelUtils.read(file, VoteUserImportExcelVO.class);
        return success(userInfoService.importVoteUsers(list, updateSupport));
    }


    @GetMapping("/get-import-template")
    @ApiOperation("获得导入用户模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 输出
        ExcelUtils.write(response, "用户导入模板.xls", "用户列表", VoteUserImportExcelVO.class, null);
    }


    @PostMapping("/change-user-statue")
    @ApiOperation("切换用户状态")
    public CommonResult<Boolean> changeUserStatue(@Valid @RequestBody VoteUserInfoUpdateReqVO reqVO) {
        VoteUserInfoDO info = userInfoService.getUserInfo(reqVO.getId());
        if(info==null){
            throw exception(USER_INFO_NOT_EXISTS);
        }
        VoteUserInfoUpdateReqVO update = new VoteUserInfoUpdateReqVO();
        update.setId(info.getId());
        update.setIsDisable(!info.getIsDisable());
        userInfoService.updateUserInfo(update);
        return success(true);
    }

}
